package cate.game.activity.dailyrecharge;

import cate.common.table.activity.base.RechargeAddUpBaseRow;
import cate.common.table.d.GDFunc;
import cate.common.table.d.GDOperation;
import cate.common.util.GameResult;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.dailyrecharge.msg.RechargeAddUpInfoResp;
import cate.game.mail.po.MailBuilder;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import cate.game.role.mail.Mail;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.HashMap;
import java.util.Map;

@NoteClass("累计充值活动")
public class RechargeAddUpActivity extends RoleActivitySingle {
    @NoteField("累充数额")
    public long paySum;
    @NoteField("领取状态")
    public Map<Integer, Boolean> gotMap;
    @NoteField("当前活动天数")
    public int nowDay;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (gotMap == null) {
            gotMap = new HashMap<>();
        }
        role.getActivity().registerRecharge(this, this::onPay);
    }

    @Override
    public void excelCheck() {
        Map<Integer, RechargeAddUpBaseRow> configs = getConfigs();
        if (configs == null || configs.isEmpty()) {
            gotMap.clear();
            return;
        }
        gotMap.entrySet().removeIf(entry -> !configs.containsKey(entry.getKey()));
        for (RechargeAddUpBaseRow config : configs.values()) {
            if (!gotMap.containsKey(config.id)) {
                gotMap.put(config.id, false);
            }
        }
    }

    public void clear() {
        paySum = 0;
        gotMap.clear();
    }

    public void noticeUpdate() {
        role.sendNow(new RechargeAddUpInfoResp(this));
    }

    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
        nowDay = getCurrentDay();
        super.onDaySpan(silence);
        switch (funcId()) {
            case GDFunc.ACT_DAILY_RECHARGE: {
                // 每日充值 每天刷新奖励
                checkAutoMail();
                clear();
                excelCheck();
                if (!silence) {
                    noticeUpdate();
                }
            }
                break;
            default:
                break;
        }
    }

    @Override
    public void onRoleOpen() {

    }

    @Override
    public void onRoleEnd() {
        checkAutoMail();
        clear();
    }

    @Override
    public void initSend() {
        noticeUpdate();
    }

    /**
     * 获取操作类型
     */
    @JsonIgnore
    private int getGDOperation() {
        switch (funcId()) {
            case GDFunc.ACT_DAILY_RECHARGE:
                return GDOperation.ACT_DAILY_RECHARGE;
            case GDFunc.LIMIT_TIME_RECHARGE:
                return GDOperation.LIMIT_RECHARGE;
            default:
                return GDOperation.NULL;
        }
    }

    /**
     * 获取所有配置
     */
    @JsonIgnore
    private Map<Integer, RechargeAddUpBaseRow> getConfigs() {
        if (nowDay == 0) {
            nowDay = getCurrentDay();
        }
        switch (funcId()) {
            case GDFunc.ACT_DAILY_RECHARGE: {
                return role.getGame().table.dailyRecharge.getConfigs(configTid(), nowDay);
            }
            case GDFunc.LIMIT_TIME_RECHARGE:
                return role.getGame().table.limitRecharge.limitRecharge.getConfigs(configTid());
            default:
                return null;
        }
    }

    /**
     * 根据配置ID获取配置
     */
    @JsonIgnore
    private RechargeAddUpBaseRow getConfig(int tid) {
        switch (funcId()) {
            case GDFunc.ACT_DAILY_RECHARGE:
                return role.getGame().table.dailyRecharge.item.get(tid);
            case GDFunc.LIMIT_TIME_RECHARGE:
                return role.getGame().table.limitRecharge.limitRecharge.get(tid);
            default:
                return null;
        }
    }

    /**
     * 充值触发
     *
     * @param payNum 充值数
     */
    public void onPay(long payNum) {
        paySum += payNum;
        noticeUpdate();
    }

    /**
     * 领取奖励
     *
     * @param tid 配置ID
     * @return 领取结果 携带活动数据本体
     */
    public GameResult<RechargeAddUpActivity> getReward(int tid) {
        GameResult<RechargeAddUpActivity> r = new GameResult<>();
        r.data = this;
        if (!gotMap.containsKey(tid)) {
            return r.fail("配置不存在");
        }
        RechargeAddUpBaseRow config = getConfig(tid);
        if (config == null) {
            return r.fail("不存在配置");
        }
        if (gotMap.get(tid)) {
            return r.fail("已领取该奖励");
        }
        if (paySum < config.paySum) {
            return r.fail("累充金额不足");
        }
        MixRes rewardRes = new MixRes(config.rewardStr);
        if (rewardRes.isEmpty()) {
            return r.fail("奖励配置错误");
        }
        gotMap.put(tid, true);
        EcResult<?> exe = new MixResAdder().setRes(rewardRes)
                .setOperation(getGDOperation())
                .setOperationSub(tid)
                .setSendMailWhenFull(true)
                .setDialogReward(true)
                .exe(role);
        return r.success();
    }

    /**
     * 检测发邮件奖励
     */
    public void checkAutoMail() {
        for (Map.Entry<Integer, Boolean> entry : gotMap.entrySet()) {
            if (!entry.getValue()) {
                RechargeAddUpBaseRow config = getConfig(entry.getKey());
                if (config != null && config.mailId != 0 && paySum >= config.paySum) {
                    entry.setValue(true);
                    MixRes rewardRes = new MixRes(config.rewardStr);
                    if (!rewardRes.isEmpty()) {
                        Mail mail = new MailBuilder().setReward(rewardRes)
                                .setContentId(config.mailId)
                                .setOperation(getGDOperation())
                                .setOperationSub(entry.getKey())
                                .build();
                        role.getMail().add(mail);
                    }
                }
            }
        }
    }
}
